<?php
//  This file is part of EkkinCMS.
//
//  EkkinCMS is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  EkkinCMS is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with EkkinCMS.  If not, see <http://www.gnu.org/licenses/>.

$current_page = $_POST["edit-id"];

// Edit menu item

$r = $db->query( "SELECT * FROM menu" );
$id = $current_page;
foreach ( $r as $key ){
    if ( $key[id] == $id ){
        $menu_title = $key[title];
        $menu_rank = $key[position];
        $menu_parent = $key[parent];
        break;
    }
}

$new_title = htmlentities($_POST["edit-menu-title"], ENT_QUOTES);
$new_rank = $_POST["edit-menu-rank"];
$new_parent = $_POST["edit-menu-parent"];
if($_POST["edit-hidden"] == "on"){
    $new_hidden = 1;
}
echo $new_hidden;
//echo "WHERE: $menu_rank, $menu_title, $id";
$query = "
UPDATE menu
    SET
        position = '$new_rank',
        title = '$new_title',
        id = '$id',
        parent = '$new_parent',
        hidden = '$new_hidden',
        lang = 'en'
WHERE
    position = '$menu_rank' AND
    title = '$menu_title' AND
    id = '$id' AND
    parent = '$menu_parent' AND
    (lang = 'en_US' OR lang = 'en')";

//print $query;

$db->exec($query);

// Edit page
$r = $db->get_values_c( "pages", "id = $current_page AND (lang = \"en\"  OR lang = \"en_US\")" );
$site_content = $r[content];
$site_title = $r[title];

$new_title = htmlentities($_POST["edit-title"], ENT_QUOTES);
$new_content = htmlentities($_POST["edit-content"], ENT_QUOTES);

$query = "
UPDATE pages
    SET
        id = '$id',
        title = '$new_title',
        content = '$new_content',
        lang = 'en'
 WHERE
    id = '$id' AND
    (lang = 'en_US' OR lang = 'en')";

$db->exec($query);


// Default page?
$v = $_POST["edit-default"];

if ( $v == "on" ){
    $db->exec( "UPDATE config SET value = '$id' WHERE name = 'site_default'" );
}

$edit = false;
$link = rtrim(construct_link(array("action")), "&");

echo "<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=$link\">";
die();


?>
